<template>
  <el-dialog
    class="goods-upload-modal"
    :title="title"
    :visible.sync="importModal"
  >
    <div
      class="erorr-box"
      v-if="uploadMsg.length > 0"
      :closable="false"
      type="error"
    >
      <div style="color: #f56c6c">
        <div style="position: absolute; right: 10px; top: 10px">
          <i class="el-icon-close" @click="uploadMsg = []" />
        </div>
        <div
          v-for="(item, index) in uploadMsg"
          :key="index"
          v-html="item"
          style="padding-right: 40px"
        ></div>
      </div>
    </div>
    <div class="upload-style">
      <el-upload
        v-loading="loading"
        class="upload-demo"
        drag
        :file-list="fileList"
        :auto-upload="true"
        :action="baseUrl + actionUrl"
        :on-progress="handleUploadProress"
        accept=".xlsx,.xls"
        :data="params"
        :on-error="handleUploadError"
        :on-success="handleUploadSuccess"
        :headers="headers"
        multiple
        style="text-align: center"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          {{ $t('common.uploadMsg') }}
          <em>{{ $t('common.clickUpload') }}</em>
        </div>
      </el-upload>
    </div>
    <span
      slot="footer"
      class="dialog-footer"
      style="display: flex; justify-content: space-around"
    >
      <el-link v-if="tempalteUrl" type="primary" :href="tempalteUrl">
        {{ $t('common.downloadTemp') }}
      </el-link>
      <el-button type="text" @click="importModal = false">{{ $t('common.cancel') }}</el-button>
    </span>
  </el-dialog>
</template>
  <script>
import { getToken } from "@/utils/auth";
import { isRelogin } from '@/utils/request'
import { MessageBox } from 'element-ui'

export default {
  props: {
    title: {
      type: String,
      default: "",
    },
    actionUrl: {
      type: String,
      default: "",
    },
    tempalteUrl: {
      type: String,
      default: "",
    },
    params: {
      type: Object,
      default: () => {
        return {};
      },
    },
    // 导入成功后跳转的路由
    routerPath: {
      type: String,
      default: "/wms/receiptOrder",
    },
  },
  data() {
    return {
      headers: {
        Authorization: getToken(),
      },
      fileList: [],
      uploadMsg: [],
      importModal: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: false,
    };
  },
  methods: {
    open() {
      this.importModal = true;
      this.fileList = [];
    },
    cancel() {
      this.importModal = false;
    },
    handleUploadProress(e, file, fileList) {
      this.loading = true;
    },
    handleUploadSuccess(res, file) {
      console.log('res', res)
      this.loading = false;
      if (res.code === 200 || res.length === 0) {
        this.uploadMsg = (res.data || []).slice(0, 10);
        if (this.uploadMsg.length === 0) {
          this.$message({
            type: "success",
            message: "导入成功",
          });
          this.$router.push({ path: this.routerPath });
          this.$emit('finish')
          this.importModal = false;
        }
      } else {
        if (res.code === 401) {
          MessageBox.confirm('登录状态已过期，您可以继续留在该页面，或者重新登录', '系统提示', {
            confirmButtonText: '重新登录',
            cancelButtonText: this.$t('common.cancel'),
            type: 'warning'
          }
        ).then(() => {
          isRelogin.show = false;
          this.$store.dispatch('LogOut').then(() => {
            location.href = '/wms';
          })
        }).catch(() => {
          isRelogin.show = false;
        });
      }
        this.$message({
          type: "error",
          message: res.join(','),
        });
        this.fileList = [];
      }
    },
    handleUploadError() {
      this.loading = false;
      this.fileList = [];
      this.$message({
        type: "error",
        message: this.$t('common.failedMsg'),
      });
    },
  },
};
</script>
  